package com.gcstorage.tvdev.util;

import android.content.Context;
import android.content.SharedPreferences;


import com.gcstorage.tvdev.base.BaseApplication;

import java.util.Set;

/**
 * created by Sanpark on 2020-07-07 10:31
 */
public class SpUtils {
    /**
     * 保存在手机里面的文件名
     */
    private static final String FILE_NAME = "gc_portal";
    public static final String TAG_USER = FILE_NAME + "_user";//用户级别sp
    public static final String TAG_APP = FILE_NAME + "_app";//应用级别sp
    public static final String TAG_BASE = "baseSpf";

    private SpUtils() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static SharedPreferences getSharedPreferences(String tag) {
        return BaseApplication.getInstance().getSharedPreferences(tag, Context.MODE_PRIVATE);
    }

    /**
     * 存储 String 类型的值
     *
     * @param key
     * @param value
     */
    public static void putString(String tag, String key, String value) {
        SharedPreferences sp = getSharedPreferences(tag);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, value);
//        editor.commit();
        editor.apply();
    }

    public static void putString(String key, String value) {
        putString(TAG_APP, key, value);
    }

    /**
     * 获取 String 类型的值
     *
     * @param key
     * @param defValue 默认值，SharedPreferences中没有存储传入的key的值时返回该默认值
     * @return
     */
    public static String getString(String tag, String key, String defValue) {
        SharedPreferences sp = getSharedPreferences(tag);
        return sp.getString(key, defValue);
    }

    public static String getString(String key, String defValue) {
        return getString(TAG_APP, key, defValue);
    }

    /**
     * 获取 String 类型的值，默认返回空字符传
     *
     * @param key
     * @return
     */
    public static String getString(String key) {
        return getString(key, "");
    }

    /**
     * 存储 int 类型的值
     *
     * @param key
     * @param value
     */
    public static void putInt(String tag, String key, int value) {
        SharedPreferences sp = getSharedPreferences(tag);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, value);
//        editor.commit();
        editor.apply();
    }

    public static void putInt(String key, int value) {
        putInt(TAG_APP, key, value);
    }


    /**
     * 获取 int 类型的值
     *
     * @param key
     * @param defValue 默认值，SharedPreferences中没有存储传入的key的值时返回该默认值
     * @return
     */
    public static int getInt(String tag, String key, int defValue) {
        SharedPreferences sp = getSharedPreferences(tag);
        return sp.getInt(key, defValue);
    }

    public static int getInt(String key, int defValue) {
        return getInt(TAG_APP, key, defValue);
    }

    /**
     * 获取 int 类型的值，默认返回-1
     *
     * @param key
     * @return
     */
    public static int getInt(String key) {
        return getInt(key, 0);
    }

    public static void putFloat(String tag, String key, float value) {
        SharedPreferences sp = getSharedPreferences(tag);
        SharedPreferences.Editor editor = sp.edit();
        editor.putFloat(key, value);
//        editor.commit();
        editor.apply();
    }

    public static void putFloat(String key, float value) {
        putFloat(TAG_APP, key, value);
    }

    public static float getFloat(String tag, String key, float defValue) {
        SharedPreferences sp = getSharedPreferences(tag);
        return sp.getFloat(key, defValue);
    }

    public static float getFloat(String key, int defValue) {
        return getFloat(TAG_APP, key, defValue);
    }

    /**
     * 获取 int 类型的值，默认返回-1
     *
     * @param key
     * @return
     */
    public static float getFloat(String key) {
        return getFloat(key, 0);
    }

    /**
     * 存储 boolean 类型的值
     *
     * @param key
     * @param value
     */
    public static void putBoolean(String tag, String key, boolean value) {
        SharedPreferences sp = getSharedPreferences(tag);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, value);
//        editor.commit();
        editor.apply();
    }

    public static void putBoolean(String key, boolean value) {
        putBoolean(TAG_APP, key, value);
    }

    /**
     * 获取 boolean 类型的值
     *
     * @param key
     * @param defValue 默认值，SharedPreferences中没有存储传入的key的值时返回该默认值
     * @return
     */
    public static boolean getBoolean(String tag, String key, boolean defValue) {
        SharedPreferences sp = getSharedPreferences(tag);
        return sp.getBoolean(key, defValue);
    }

    public static boolean getBoolean(String key, boolean defValue) {
        return getBoolean(TAG_APP, key, defValue);
    }

    public static boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    /**
     * 存储StringSet类型的值
     *
     * @param key
     */
    public static void putStringSet(String tag, String key, Set<String> value) {
        SharedPreferences sp = getSharedPreferences(tag);
        SharedPreferences.Editor editor = sp.edit();
        editor.putStringSet(key, value);
        editor.apply();
    }

    public static void putStringSet(String key, Set<String> value) {
        putStringSet(TAG_APP, key, value);
    }

    /**
     * 获取StringSet类型的值，默认返回null
     *
     * @param key
     * @return
     */
    public static Set<String> getStringSet(String key) {
        return getStringSet(TAG_APP, key);
    }

    public static Set<String> getStringSet(String tag, String key) {
        SharedPreferences sp = getSharedPreferences(tag);
        return sp.getStringSet(key, null);
    }

    /**
     * 存储任意类型的值
     *
     * @param key
     * @param value
     */
    public static void put(String tag, String key, Object value) {
        SharedPreferences sp = getSharedPreferences(TAG_APP);
        SharedPreferences.Editor editor = sp.edit();
        if (value instanceof String) {
            editor.putString(key, (String) value);
        } else if (value instanceof Integer) {
            editor.putInt(key, (Integer) value);
        } else if (value instanceof Boolean) {
            editor.putBoolean(key, (Boolean) value);
        } else if (value instanceof Float) {
            editor.putFloat(key, (Float) value);
        } else if (value instanceof Long) {
            editor.putLong(key, (Long) value);
        } else {
            editor.putString(key, value.toString());
        }
        editor.apply();
    }

    public static void put(String key, Object value) {
        put(TAG_APP, key, value);
    }

    /**
     * 根据key值获取SharedPreferences 中存储的值
     *
     * @param key
     * @param defValue 默认值
     * @return
     */
    public static Object get(String tag, String key, Object defValue) {
        SharedPreferences sp = getSharedPreferences(tag);
        if (defValue instanceof String) {
            return sp.getString(key, (String) defValue);
        } else if (defValue instanceof Integer) {
            return sp.getInt(key, (Integer) defValue);
        } else if (defValue instanceof Boolean) {
            return sp.getBoolean(key, (Boolean) defValue);
        } else if (defValue instanceof Float) {
            return sp.getFloat(key, (Float) defValue);
        } else if (defValue instanceof Long) {
            return sp.getLong(key, (Long) defValue);
        }
        return null;
    }

    public static Object get(String key, Object defValue) {
        return get(TAG_APP, key, defValue);
    }

    /**
     * 移除某个 key 对应的数据
     *
     * @param key
     * @return true-移除成功；false-移除失败
     */
    public static boolean remove(String tag, String key) {
        SharedPreferences sp = getSharedPreferences(tag);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        return editor.commit();
    }

    public static boolean remove(String key) {
        return remove(TAG_APP, key);
    }


    /**
     * 清除所有数据
     *
     * @return true-清除成功；false-清除失败
     */
    public static boolean clear(String tag) {
        SharedPreferences sp = getSharedPreferences(tag);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        return editor.commit();
    }

    public static boolean clear() {
        return clear(TAG_APP);
    }
}
